import React from 'react';
import {withRouter} from 'react-router';
import { Button, Tabs, Card, Tooltip, Row, Col, Spin, Menu, Select, Modal } from 'antd';
const Option = Select.Option;
import { inject, observer } from 'mobx-react';
import { WeaTab, WeaTop, WeaSelect, WeaSteps, WeaNewScroll, WeaDropdown, WeaTools, WeaRightMenu } from 'ecCom';
import { toJS } from "mobx";
const { SubMenu } = Menu;
const Step = WeaSteps.Step;
const TabPane = Tabs.TabPane;
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
import TaskList from '../common/TaskList'
//项目思维导图
import MindMapPage from "../single/mindmap/MindMapPage";
import TaskBoard from '../single/taskBoard/taskBoard';
import TaskCardNew from "../single/task/TaskCardNew";
import RelateListDialog from "../dialog/relateListDialog"
import taskBatchEditDialogStore from '../../stores/taskBatchEditDialogStore';
import "./index.less";
@inject("projectCardStore")
@withRouter
@observer
class TaskView extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            selectkey : "1"
        };
    }

    componentWillReceiveProps(nextprops) {
        const {prjid} = this.props;
        if(prjid==''||prjid==null){
            if (this.props.location.key !== nextprops.location.key) {
                const { projectCardStore } = nextprops;
                const { getRightMenu } = projectCardStore;
                getRightMenu({type:'taskview'});
            }
        }else{
            if (this.props.prjid !== nextprops.prjid) {
                const { projectCardStore } = nextprops;
                const { getRightMenu } = projectCardStore;
                getRightMenu({type:'taskview'});
            }
        }
    }

    render() {
        const { location,projectCardStore:{ taskInfoStore, canEdit, prjCardStore, taskListStore, getRightMenu},fromPortal } = this.props;
        let prjid = this.props.prjid;
        if(prjid==''||prjid==null){
            prjid = location.query.prjid;
        }
        const account = WeaTools.ls.getJSONObj('theme-account');
        const height = document.body.offsetHeight - 122;
        let scrollHeight = ($(".e9theme-layout-header").length > 0 || $(".e8theme-layout-header").length > 0) ? height - 55 : height;
        if(fromPortal){
            scrollHeight = scrollHeight - 51;
        }
        return (
            <div style={{height:"100%"}}>
                <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@rbbol5`} style={{padding:"0px",height:"100%"}}>
                    <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@ta6k3e`}
                        buttons={this.getButtons()}
                        showDropIcon={true}
                        dropMenuDatas={this.getRightMenu()}
                        onDropMenuClick={this.onRightMenuClick.bind(this)}
                    />
                    <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@0pl174`} style={{padding:"0px",backgroundColor:"white",height:"calc(100%-51px)"}}>
                        <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@rqt0w3`}  height={scrollHeight}>
                            { this.state.selectkey == "1" && <TaskCardNew ecId={`${this && this.props && this.props.ecId || ''}_TaskCardNew@4sme7a`} prjid={prjid} fromPortal={fromPortal} hasList={true} taskInfoStore={taskInfoStore} delCallBack={() =>{getRightMenu({type:'taskview'});}}/>}
                            { this.state.selectkey == "2" && <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@5lm97a`} datas={this.getRightMenu()} onClick={this.onRightMenuClick.bind(this)}>
                                <TaskList ecId={`${this && this.props && this.props.ecId || ''}_TaskList@i00w4q`} contentStore={prjCardStore} account={account} taskBatchEditDialogStore={taskBatchEditDialogStore} delCallBack={() =>{getRightMenu({type:'tasklist'});}} />
                            </WeaRightMenu>}
                            { this.state.selectkey == "3" && <MindMapPage ecId={`${this && this.props && this.props.ecId || ''}_MindMapPage@cbz5yx`} prjid={prjid}/>}
                            { this.state.selectkey == "4" && <TaskBoard ecId={`${this && this.props && this.props.ecId || ''}_TaskBoard@hgih8o`} prjid={prjid}/>}
                            { this.state.selectkey == "5" && <iframe style={{height:"100%",width:"100%",border:"0px"}} src={"/proj/gantt/gantt.jsp?isfromProjTab=1&ProjID="+prjid} />}
                            <RelateListDialog ecId={`${this && this.props && this.props.ecId || ''}_RelateListDialog@5qu0xh`} contentStore={prjCardStore} />
                        </WeaNewScroll>
                    </Row>
                </Row>
                
            </div>
        )
    }

    addOnClick = () =>{
        const { location,projectCardStore:{ taskInfoStore } } = this.props;
        let prjid = this.props.prjid;
        if(prjid==''||prjid==null){
            prjid = location.query.prjid;
        }
        if(this.state.selectkey == "1"){
            taskInfoStore.handleDialog(true, "add", '', { prjid: prjid, parentid: "" });
        }else if(this.state.selectkey == "2"){
            prjCardStore.taskInfoStore.handleDialog(true, "add", '', { prjid: prjid, parentid: "" });
        }
    }

    getButtons = () =>{
        const {selectkey} = this.state;
        const { projectCardStore, fromPortal, changeCallBack } = this.props;
        const { rightMenu, prjid, taskInfoStore, prjCardStore, status, isApprove, getRightMenu } = projectCardStore;
        const { approveplanstatus } = prjCardStore;
        let btnArr = [];
        btnArr.push(
            <span style={{display:"flex",height:"100%",justifyContent:"flex-end",alignItems:"center"}}>
            <span style={{marginRight:"5px",color: "#606060"}}>{getLabel('19407','布局')}</span>
                <Select ecId={`${this && this.props && this.props.ecId || ''}_Select@bnwxuq`} defaultValue="1" style={{ width: 80,textAlign: 'left'}} onChange={(key)=>{this.changeOption(key)}}>
                    <Option ecId={`${this && this.props && this.props.ecId || ''}_Option@ov2zns`} value="1">{getLabel('388377','卡片')}</Option>
                    <Option ecId={`${this && this.props && this.props.ecId || ''}_Option@ycqa0m`} value="2">{getLabel('320','列表')}</Option>
                    <Option ecId={`${this && this.props && this.props.ecId || ''}_Option@lizjqf`} value="3" >{getLabel('385900','思维导图')}</Option>
                    <Option ecId={`${this && this.props && this.props.ecId || ''}_Option@m07k5y`} value="4">{getLabel('387492','看板')}</Option>
                </Select>
            </span>
        );
        rightMenu && rightMenu.length > 0 && rightMenu.map(m => {
            let disabled = false;

            if (approveplanstatus == true) {
                if (m.type == "BTN_DOSUBMIT") {
                    disabled = true;
                }
            }

            if(prjCardStore.taskInfoStore.selectedRowKeys.length == 0){
                if(m.type == "BTN_DELETEBATCH"){
                    disabled = true;
                }
            }

            if(prjCardStore.taskInfoStore.selectedRowKeys.length == 0){
                if(m.type == "BTN_SUBMITEDIT"){
                    disabled = true;
                }
            }

            if((status == '-1' || isApprove)){
                if(m.type == "BTN_BREAKDOWN"){
                    disabled = true;
                }
            }

            m.isTop == '1' && btnArr.length < 4 && btnArr.push(
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@b2pv4y@${m.type}`} type="primary"
                    disabled={disabled}
                    onClick={() => {
                        if (m.type == "BTN_ADDTASK") { //添加任务
                            if(selectkey == "1"){
                                taskInfoStore.handleDialog(true, "add", '', { prjid: prjid, parentid: "" });
                            }else if(selectkey == "2"){
                                prjCardStore.taskInfoStore.handleDialog(true, "add", '', { prjid: prjid, parentid: "" });
                            }
                        }else if(m.type == "BTN_DELETEBATCH" ){   //批量删除
                            prjCardStore.delTaskBatch("delbatch",{callBack: ()=> {
                                if(selectkey == "2"){
                                    getRightMenu({type:'tasklist'});
                                }
                            }});
                        }else if(m.type == "BTN_BREAKDOWN" ){   //任务分解
                            prjCardStore.triggerBreakDownWF({callBack: ()=> {
                                if(fromPortal){
                                    changeCallBack();
                                }
                                if(selectkey == "1"){
                                    getRightMenu({type:'taskview'});
                                }else if(selectkey == "2"){
                                    getRightMenu({type:'tasklist'});
                                }
                            }});
                        }else if (m.type == 'BTN_DOSUBMIT') { //提交执行
                            Modal.confirm({
                                title: getLabel(15172, "系统提示"),
                                content: getLabel(83919, "你确定要提交执行吗？"),
                                onOk() {
                                    prjCardStore.doPlanOpt_task({ method: 'approveplan', prjid: prjid, callBack: ()=> {
                                        if(fromPortal){
                                            changeCallBack();
                                        }
                                        if(selectkey == "1"){
                                            getRightMenu({type:'taskview'});
                                        }else if(selectkey == "2"){
                                            getRightMenu({type:'tasklist'});
                                        }
                                    } });
                                },
                                onCancel() { },
                            });
                        }else if (m.type == 'BTN_APPSUBMIT') { //提交执行(审批)
                            Modal.confirm({
                                title: getLabel(15172, "系统提示"),
                                content: getLabel(83921, "你确定要提交审批吗？"),
                                onOk() {
                                    prjCardStore.doPlanOpt_task({ method: 'submitplan', prjid: prjid, callBack: ()=> {
                                        if(fromPortal){
                                            changeCallBack();
                                        }
                                        if(selectkey == "1"){
                                            getRightMenu({type:'taskview'});
                                        }else if(selectkey == "2"){
                                            getRightMenu({type:'tasklist'});
                                        }
                                    } });
                                },
                                onCancel() { },
                            });
                        }else if(m.type == "BTN_SUBMITEDIT" ){   //提交编辑
                            taskBatchEditDialogStore.setVisible(true);
                        }
                    }}
                    disabled={disabled}
                >
                    {m.menuName}
                </Button>
            );
        });
        return btnArr;
    }

    getRightMenu() {
        const { projectCardStore } = this.props;
        const { rightMenu, prjCardStore, status, isApprove } = projectCardStore;
        const { approveplanstatus } = prjCardStore;
        let btnArr = [];
        rightMenu && rightMenu.length > 0 && rightMenu.map(m => {
            let disabled = false;

            if (approveplanstatus == true) {
                if (m.type == "BTN_DOSUBMIT") {
                    disabled = true;
                }
            }

            if(prjCardStore.taskInfoStore.selectedRowKeys.length == 0){
                if(m.type == "BTN_DELETEBATCH"){
                    disabled = true;
                }
            }

            if(prjCardStore.taskInfoStore.selectedRowKeys.length == 0){
                if(m.type == "BTN_SUBMITEDIT"){
                    disabled = true;
                }
            }

            if((status == '-1' || isApprove)){
                if(m.type == "BTN_BREAKDOWN"){
                    disabled = true;
                }
            }

            btnArr.push({
                icon: <i className={m.menuIcon} />,
                content: m.menuName,
                disabled: disabled
            })
        });
        return btnArr
    }

    onRightMenuClick(key) {
        const {selectkey} = this.state;
        const { projectCardStore, fromPortal, changeCallBack } = this.props;
        const { rightMenu, prjid, taskInfoStore, prjCardStore, getRightMenu } = projectCardStore;
        const { relateListStore } = prjCardStore;
        rightMenu && rightMenu.length > 0 && rightMenu.map((m, i) => {
            if (Number(key) == i) {
                if (m.type == "BTN_ADDTASK") {  //添加任务
                    if(selectkey == "1"){
                        taskInfoStore.handleDialog(true, "add", '', { prjid: prjid, parentid: "" });
                    }else if(selectkey == "2"){
                        prjCardStore.taskInfoStore.handleDialog(true, "add", '', { prjid: prjid, parentid: "" });
                    }
                } else if (m.type == 'BTN_DOSUBMIT') { //提交执行
                    Modal.confirm({
                        title: getLabel(15172, "系统提示"),
                        content: getLabel(83919, "你确定要提交执行吗？"),
                        onOk() {
                            prjCardStore.doPlanOpt_task({ method: 'approveplan', prjid: prjid, callBack: ()=> {
                                if(fromPortal){
                                    changeCallBack();
                                }
                                if(selectkey == "1"){
                                    getRightMenu({type:'taskview'});
                                }else if(selectkey == "2"){
                                    getRightMenu({type:'tasklist'});
                                }
                            } });
                        },
                        onCancel() { },
                    });
                } else if (m.type == 'BTN_APPSUBMIT') { //提交执行(审批)
                    Modal.confirm({
                        title: getLabel(15172, "系统提示"),
                        content: getLabel(83921, "你确定要提交审批吗？"),
                        onOk() {
                            prjCardStore.doPlanOpt_task({ method: 'submitplan', prjid: prjid, callBack: ()=> {
                                if(fromPortal){
                                    changeCallBack();
                                }
                                if(selectkey == "1"){
                                    getRightMenu({type:'taskview'});
                                }else if(selectkey == "2"){
                                    getRightMenu({type:'tasklist'});
                                }
                            } });
                        },
                        onCancel() { },
                    });
                } else if (m.type == 'BTN_TASKEXPORT') { //导出任务
                    prjCardStore.doTaskExcelExp({ prjid: prjid });
                } else if (m.type == 'BTN_SAVEPLAN') { //存为计划版本
                    prjCardStore.saveasplan({ method: 'saveasplan', prjid: prjid },() =>{prjCardStore.initData(prjid);});
                } else if (m.type == 'BTN_TASKHISTORY') { //历史版本
                    relateListStore.handleRelateDialog(true, "history", prjid)
                } else if (m.type == 'BTN_NOTEMEMBER') { //通知成员
                    if(selectkey == "1"){
                        relateListStore.handleRelateDialog(true, "notice", prjid);
                    }else if(selectkey == "2"){
                        prjCardStore.relateListStore.handleRelateDialog(true, "notice", prjid);
                    }
                } else if (m.type == 'BTN_ONREQUEST') { //相关流程
                    if(selectkey == "1"){
                        relateListStore.handleRelateDialog(true, "request", prjid);
                    }else if(selectkey == "2"){
                        prjCardStore.relateListStore.handleRelateDialog(true, "request", prjid);
                    }
                } else if (m.type == 'BTN_ONDOC') { //相关文档
                    if(selectkey == "1"){
                        relateListStore.handleRelateDialog(true, "doc", prjid);
                    }else if(selectkey == "2"){
                        prjCardStore.relateListStore.handleRelateDialog(true, "doc", prjid);
                    }
                } else if (m.type == 'BTN_ONCOWORK') { //相关协作
                    if(selectkey == "1"){
                        relateListStore.handleRelateDialog(true, "cowork", prjid);
                    }else if(selectkey == "2"){
                        prjCardStore.relateListStore.handleRelateDialog(true, "cowork", prjid);
                    }
                } else if (m.type == 'BTN_LOG') {  //日志
                    relateListStore.handleRelateDialog(true, "log", prjid)
                } else if(m.type == "BTN_DELETEBATCH" ){   //批量删除
                    prjCardStore.delTaskBatch("delbatch",{callBack: ()=> {
                        if(selectkey == "2"){
                            getRightMenu({type:'tasklist'});
                        }
                    }});
                } else if(m.type == "BTN_EDITBATCH" ){   //批量编辑
                    prjCardStore.setIsBatchEdit(1);
                    prjCardStore.getPrjTaskList();
                    prjCardStore.resetTaskInfoStore();
                    getRightMenu({type:'taskBatchEdit'});
                } else if(m.type == "BTN_BACK" ){   //返回
                    prjCardStore.setIsBatchEdit(0);
                    prjCardStore.getPrjTaskList();
                    prjCardStore.resetTaskInfoStore();
                    getRightMenu({type:'tasklist'});
                } else if(m.type == "BTN_SUBMITEDIT" ){   //提交编辑
                    taskBatchEditDialogStore.setVisible(true);
                } else if(m.type == "BTN_BREAKDOWN" ){   //任务分解
                    prjCardStore.triggerBreakDownWF({callBack: ()=> {
                        if(fromPortal){
                            changeCallBack();
                        }
                        if(selectkey == "1"){
                            getRightMenu({type:'taskview'});
                        }else if(selectkey == "2"){
                            getRightMenu({type:'tasklist'});
                        }
                    }});
                }
            }
        });
    }

    changeOption = (key) =>{
        const { projectCardStore, prjid } = this.props;
        const { getRightMenu, prjCardStore } = projectCardStore;
        this.setState({selectkey : key});
        if(key==1){
            getRightMenu({type:'taskview'});
        }else if(key==2){
            prjCardStore.prjid = prjid;
            prjCardStore.setIsBatchEdit(0);
            prjCardStore.getPrjTaskCondition();
            prjCardStore.getPrjTaskVersion();
            prjCardStore.getPrjTaskList();
            getRightMenu({type:'tasklist'});
        }else if(key==3){
            getRightMenu({type:'mindmap'});
        }else if(key==4){
            getRightMenu({type:'taskboard'});
        }
    }

}

export default TaskView;